<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Securimage extension
 * 
 * Allows highly customisable CAPTCHAs to be used, and
 * provides audio support.
 * 
 * This class extends the securimage class in order to 
 * make it compatible with CodeIgniter. This class requires
 * that the user has cookies enabled in order to work.
 * 
 * The original securimage source is available for download
 * from www.phpcaptcha.org/
 * 
 */
include(APPPATH . 'libraries/securimage/securimage.php');

class Si_captcha extends Securimage
{
	var $CI = NULL;
	var $test_mode = FALSE;
	
	function Si_captcha($config=array())
	{
		$this->CI =& get_instance();
		$this->CI->load->library('session'); // Load the session library in case it's not already loaded.
		
		// Load the default configuration.
		require_once(APPPATH . 'config/securimage'.EXT);
		if (isset($SI_config))
		{
			$this->initialize($SI_config);
			unset($SI_config);
		}
		
		// Now override with the users config if it's been supplied.
		$this->initialize($config);

		// Suh-weeeet...
	}
	
	function initialize($config=array())
	{
		if (is_array($config))
		{
			foreach ($config as $key=>$val)
			{
				$this->$key = $val;
			}
		}
	}
	
	function show($background_image = "")
	{
		if($background_image != "" && is_readable($background_image)) 
		{
			$this->bgimg = $background_image;
		}
	
		$this->doImage();
	}
	
	function saveData()
	{
		$this->CI->session->set_userdata('securimage_code_value', strtolower($this->code));
	}
	
	function validate()
	{
		if ($this->CI->session->userdata('securimage_code_value'))
		{
			if ($this->CI->session->userdata('securimage_code_value') == strtolower(trim($this->code_entered)))
			{
				$this->correct_code = true;
				$this->CI->session->unset_userdata('securimage_code_value');
			}
			else
			{
				$this->correct_code = false;
			}
		}
		else
		{
			$this->correct_code = false;
		}
	}
	
	function getCode()
	{
		if ($this->CI->session->userdata('securimage_code_value'))
		{
			return $this->CI->session->userdata('securimage_code_value');
		}
		else
		{
			return '';
		}
	}
}